package com.expert.miniapp.service.impl;

import com.expert.miniapp.dao.ResearchExpert;
import com.expert.miniapp.dao.ResearchExpertMapper;
import com.expert.miniapp.service.ResearchExpertService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * 科研专家业务逻辑实现类
 * 实现专家信息的业务处理功能
 */
@Service
public class ResearchExpertServiceImpl implements ResearchExpertService {

    @Autowired
    private ResearchExpertMapper researchExpertMapper;

    @Override
    public ResearchExpert getExpertById(String expertId) {
        if (expertId == null || expertId.trim().isEmpty()) {
            return null;
        }
        return researchExpertMapper.findById(expertId);
    }

    @Override
    public List<ResearchExpert> getExpertsByCategory(String category) {
        if (category == null || category.trim().isEmpty()) {
            return researchExpertMapper.findAll();
        }
        return researchExpertMapper.findByCategory(category);
    }

    @Override
    public PageInfo<ResearchExpert> getAllExperts(Integer pageNum, Integer pageSize) {
        pageNum = (pageNum == null || pageNum < 1) ? 1 : pageNum;
        pageSize = (pageSize == null || pageSize < 1) ? 10 : pageSize;
        PageHelper.startPage(pageNum, pageSize);
        List<ResearchExpert> experts = researchExpertMapper.findAll();
        return new PageInfo<>(experts);
    }

    @Override
    public PageInfo<ResearchExpert> getRecommendedExperts(Integer pageNum, Integer pageSize) {
        pageNum = (pageNum == null || pageNum < 1) ? 1 : pageNum;
        pageSize = (pageSize == null || pageSize < 1) ? 10 : pageSize;
        PageHelper.startPage(pageNum, pageSize);
        List<ResearchExpert> experts = researchExpertMapper.findByWeightOrder();
        return new PageInfo<>(experts);
    }

    @Override
    public PageInfo<ResearchExpert> searchExperts(String keyword, Integer pageNum, Integer pageSize) {
        pageNum = (pageNum == null || pageNum < 1) ? 1 : pageNum;
        pageSize = (pageSize == null || pageSize < 1) ? 10 : pageSize;
        PageHelper.startPage(pageNum, pageSize);
        List<ResearchExpert> experts = researchExpertMapper.searchByKeyword(keyword);
        return new PageInfo<>(experts);
    }
}